package com.jk.kdd.console.controller;

import com.jk.kdd.base.BaseController;
import com.jk.kdd.console.model.SystemVariable;
import com.jk.kdd.console.model.SystemVariableClassify;
import com.jk.kdd.console.service.SystemVariableClassifyService;
import com.jk.kdd.console.service.SystemVariableService;
import com.jk.kdd.console.vo.SystemVariableClassifyVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.framework.util.CastUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author hanping.fu
 * @project jk-console
 * @data 2017/03/07
 * @desc
 */
@Controller
@RequestMapping("/console")
public class SystemVariableController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SystemVariableController.class);

    @Resource(name = "systemVariableClassifyService")
    private SystemVariableClassifyService systemVariableClassifyService;

    @Resource(name = "systemVariableService")
    private SystemVariableService systemVariableService;


    /**
     * 变量总分类列表
     */
    @RequestMapping(value = "/systemVariableList", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> systemVariableList() {
        List<SystemVariableClassifyVO> systemVariableClassifyList = systemVariableClassifyService.selectByCondition();
        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();
        param.put("code", SUCCESS_CODE);
        param.put("msg", "操作成功！");
        data.put("systemVariableClassifyList", systemVariableClassifyList);
        param.put("data", data);
        return param;
    }


    /**
     * 变量总分类进行增删改查、排序 操作
     */

    //新增
    @RequestMapping(value="",method= RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addVariable() {

        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();

        // 操作人ID
        String operateId = getSessionStringValue("operateId");
        //类型id
        String classifyId = getRequestStringValue("classifyId");
        //类型名称
        String classifyName = getRequestStringValue("classifyName");

        boolean flag = systemVariableClassifyService.createVariable(classifyId,classifyName, operateId);

        if (flag) {
            param.put("code", SUCCESS_CODE);
            param.put("msg", "操作成功！");
            param.put("data", data);
        } else {
            param.put("code", ERROR_CODE);
            param.put("msg", "操作失败！");
            param.put("data", data);
            return param;

        }

        return param;
    }



    //修改
    @RequestMapping(value="/modify",method= RequestMethod.PUT)
    @ResponseBody
    public Map<String,Object> modifyVariable() {

        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();

        // 操作人ID
        String operateId = getSessionStringValue("operateId");
        //分类类型
        String id = getRequestStringValue("id");
        //类型id
        String classifyId = getRequestStringValue("classifyId");
        //类型名称
        String classifyName = getRequestStringValue("classifyName");

        boolean flag = systemVariableClassifyService.modifyVariable(id,classifyId,classifyName, operateId);

        if (flag) {
            param.put("code", SUCCESS_CODE);
            param.put("msg", "操作成功！");
            param.put("data", data);
        } else {
            param.put("code", ERROR_CODE);
            param.put("msg", "操作失败！");
            param.put("data", data);
            return param;

        }

        return param;
    }



    //删除
    @RequestMapping(value="/delete",method= RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> deleteVariable() {

        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();

        // 操作人ID
        String operateId = getSessionStringValue("operateId");
        //分类类型
        String id = getRequestStringValue("id");

        boolean flag = systemVariableClassifyService.deleteVariable(id, operateId);

        if (flag) {
            param.put("code", SUCCESS_CODE);
            param.put("msg", "操作成功！");
            param.put("data", data);
        } else {
            param.put("code", ERROR_CODE);
            param.put("msg", "操作失败！");
            param.put("data", data);
            return param;

        }

        return param;
    }


    /**
     * 变量总分类列表排序
     */
    @RequestMapping(value = "/sortVariable", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String,Object> sortVariableList() {

        String id1 = getRequestStringValue("id1");
        String id2 = getRequestStringValue("id2");

        String sNum1 = getRequestStringValue("sNum1");
        String sNum2 = getRequestStringValue("sNum2");

        // 操作人ID
        String operateId = getSessionStringValue("operateId");

        boolean flag = systemVariableClassifyService.sortVariableList(id1, id2, sNum1, sNum2, operateId);
        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();
        if (flag) {
            param.put("code", SUCCESS_CODE);
            param.put("msg", "操作成功！");
            param.put("data", data);
        } else {
            param.put("code", ERROR_CODE);
            param.put("msg", "操作失败！");
            param.put("data", data);
            return param;

        }

        return param;
    }


    /**
     * 根据分类类型查询分类下列表
     */
    @RequestMapping(value = "/classifyListByType", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> classifyListByType() {
        //分类类型
        String classifyType = getRequestStringValue("classifyType");
        List<SystemVariable> systemVariableClassifyList = systemVariableService.getClassifyByType(classifyType);
        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();
        param.put("code", SUCCESS_CODE);
        param.put("msg", "操作成功！");
        data.put("systemVariableClassifyList", systemVariableClassifyList);
        param.put("data", data);
        return param;
    }


    /**
     * 分类下列表进行增删改查、排序 操作
     */

    //新增
    @RequestMapping(value="",method= RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addClassify() {

        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();

        // 操作人ID
        String operateId = getSessionStringValue("operateId");
        //分类类型
        String varType = getRequestStringValue("varType");
        //名称
        String varName = getRequestStringValue("varName");

        boolean flag = systemVariableService.createClassify(varType, varName,operateId);

        if (flag) {
            param.put("code", SUCCESS_CODE);
            param.put("msg", "操作成功！");
            param.put("data", data);
        } else {
            param.put("code", ERROR_CODE);
            param.put("msg", "操作失败！");
            param.put("data", data);
            return param;

        }

        return param;
    }



    //修改
    @RequestMapping(value="/modify",method= RequestMethod.PUT)
    @ResponseBody
    public Map<String,Object> modifyClassify() {

        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();

        // 操作人ID
        String operateId = getSessionStringValue("operateId");
        //分类类型
        String varType = getRequestStringValue("varType");
        //varKey
        String varKey = getRequestStringValue("varKey");

        //名称
        String varName = getRequestStringValue("varName");

        boolean flag = systemVariableService.modifyClassify(varType,varKey,varName, operateId);

        if (flag) {
            param.put("code", SUCCESS_CODE);
            param.put("msg", "操作成功！");
            param.put("data", data);
        } else {
            param.put("code", ERROR_CODE);
            param.put("msg", "操作失败！");
            param.put("data", data);
            return param;

        }

        return param;
    }



    //删除
    @RequestMapping(value="/delete",method= RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> deleteClassify() {

        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();

        // 操作人ID
        String operateId = getSessionStringValue("operateId");
        //分类类型
        String varType = getRequestStringValue("classifyType");
        //varKey
        String varKey = getRequestStringValue("varKey");

        boolean flag = systemVariableService.deleteClassify(varType,varKey, operateId);

        if (flag) {
            param.put("code", SUCCESS_CODE);
            param.put("msg", "操作成功！");
            param.put("data", data);
        } else {
            param.put("code", ERROR_CODE);
            param.put("msg", "操作失败！");
            param.put("data", data);
            return param;

        }

        return param;
    }



    /**
     * 变量分类列表排序
     */
    @RequestMapping(value = "/sortClassify", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String,Object> sortClassifyList() {
        // 操作人ID
        String operateId = getSessionStringValue("operateId");
        //分类类型
        String varType1 = getRequestStringValue("classifyType1");
        //varKey
        String varKey1 = getRequestStringValue("varKey1");
        String varType2 = getRequestStringValue("classifyType2");
        String varKey2 = getRequestStringValue("varKey2");

        String sNum1 = getRequestStringValue("sNum1");
        String sNum2 = getRequestStringValue("sNum2");


        boolean flag = systemVariableService.sortVariableList(varType1, varKey1, varType2, varKey2, sNum1, sNum2, operateId);
        HashMap<String, Object> param = new HashMap<String, Object>();
        HashMap<String, Object> data = new HashMap<String, Object>();
        if (flag) {
            param.put("code", SUCCESS_CODE);
            param.put("msg", "操作成功！");
            param.put("data", data);
        } else {
            param.put("code", ERROR_CODE);
            param.put("msg", "操作失败！");
            param.put("data", data);
            return param;

        }

        return param;
    }


}
